<?php

class FRM_Controller_Plugin_Standart extends Zend_Controller_Plugin_Abstract {
	private $myFrontController;
	private $myView;
	private $myModule;
	
	public function routeStartup($request) {
		/**
		 * Добавляем пути к контроллерам
		 */
		$this->myFrontController = Zend_Controller_Front::getInstance ();
		
		$dir = dir ( CONTROLLERS );
		while ( false !== ($module = $dir->read ()) ) {
			if ($module != '.' && $module != '..') {
				if (is_dir ( CONTROLLERS . $module )) {
					$this->myFrontController->addControllerDirectory ( CONTROLLERS . $module . '/', $module );
				}
			}
		}
	}
	
	public function dispatchLoopStartup($request) {
		$this->myFrontController->setDefaultModule ( $request->getModuleName () );
	}
	
	public function preDispatch($request) {
		$this->myFrontController = Zend_Controller_Front::getInstance ();
		$smarty = new FRM_View_Smarty ( BASE_VIEW . $request->getModuleName () . '/' );
		
		Zend_Registry::set ( 'Zend_View', $this->myView = $smarty );
		/**
		 * Настройка плагина viewRenderer
		 */
		Zend_Controller_Action_HelperBroker::getStaticHelper ( 'viewRenderer' )->setView ( $this->myView )->setViewSuffix ( 'tpl' );
		
		FRM_ACL::$currentModule = $request->getModuleName ();
		
		if ($request->getModuleName () === 'client') {
			// Подключаем словарь
			

			global $lang_base;
			
			if (isset ( $_POST ['lang'] ))
				$lang = $_POST ['lang'];
			elseif (! isset ( $_POST ['lang'] ) && isset ( $_SESSION ['global'] ['lang'] ))
				$lang = $_SESSION ['global'] ['lang'];
			else
				$lang = 'ru';
			$LangFile = PATH_BASE . "/language/dictionary/" . $lang . ".php";
			if (file_exists ( $LangFile )) {
				require_once ($LangFile);
			} else {
				//$base = & $db->getall ( "select * from " . PREFIX . "dictionary where lang='" . $_SESSION ['lang_admin'] . "'" );
				Zend_Registry::set('lang', $lang);
				$base = Admin_Dictionary::getInstance ( 'Admin_Dictionary' )->getList ();
				$handle = fopen ( $LangFile, 'w' );
				$content = "<?php\n\$lang_base=array(\n";
				
				foreach ( $base as $key => $value ) {
					$lang_base [$value ['key']] = $value ['l_value'];
					$content .= "\"" . $value ['key'] . "\"=>\"" . htmlspecialchars ( $value ['l_value'] ) . "\",\n";
				}
				
				$content .= "\"end\"=>\"\"\n);\n?>";
				
				fwrite ( $handle, $content );
				fclose ( $handle );
			}
		}
		
		if ($request->getModuleName () === 'admin') {
			
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Admin::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Admin::getInstance ();
			FRM_ACL::$currentAuth = FRM_Auth_Admin::getInstance ();
			
			if (! FRM_ACL::hasPermition ( $request->getControllerName (), $request->getActionName () )) {
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'access' );
			} else {
				$a = new Admin_Auth ( );
				try {
					$a->updateLastActivity ();
				} catch ( Exception $e ) {
				
				}
			}
		}
		
		if ($request->getModuleName () === 'operator') {
			
			$userData = FRM_Auth_Operator::getInstance ()->getStorage()->read();
			if(isset($userData->_status)){
				FRM_ACL::$currentStatus = $userData->_status;
			}
			
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Operator::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Operator::getInstance ();
			FRM_ACL::$currentAuth = FRM_Auth_Operator::getInstance ();
			
			if (! FRM_ACL::hasPermition ( $request->getControllerName (), $request->getActionName () )) {
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'access' );
			} else {
				$a = new Operator_Auth ( );
				$a->updateLastActivity ();
			}
		}
		
		if ($request->getModuleName () === 'supervisor') {
			
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Supervisor::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Supervisor::getInstance ();
			FRM_ACL::$currentAuth = FRM_Auth_Supervisor::getInstance ();
			/*
			if (! FRM_ACL::hasPermition ( $request->getControllerName (), $request->getActionName () )) {
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'access' );
			} else {
				$a = new Admin_Auth ( );
				$a->updateLastActivity ();
			}*/
		}
		
		if ($request->getModuleName () === 'cabinet') {
			FRM_Controller_Action_Abstract::$currentAuth = FRM_Auth_Agents::getInstance ();
			FRM_Db_Table_Abstract::$currentAuth = FRM_Auth_Agents::getInstance ();
		}
	
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $request
	 */
	public function postDispatch($request) {
		
		$this->myView->setBasePath ( BASE_VIEW . $request->getModuleName () . '/' );
	}
}